const pem = require('pem');
const fs = require('fs');
const path = require('path');

pem.createCertificate({ days: 365, selfSigned: true }, (err, keys) => {
  if (err) {
    console.error('生成证书时出错:', err);
    return;
  }

  const certPath = path.join(__dirname, 'ssl');
  if (!fs.existsSync(certPath)) {
    fs.mkdirSync(certPath, { recursive: true });
  }

  fs.writeFileSync(path.join(certPath, 'private.key'), keys.serviceKey);
  fs.writeFileSync(path.join(certPath, 'certificate.crt'), keys.certificate);

  console.log('证书生成成功！');
  console.log('私钥保存在:', path.join(certPath, 'private.key'));
  console.log('证书保存在:', path.join(certPath, 'certificate.crt'));
}); 